using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using mshtml;
using Dimok;
using IE = Dimok.IE;

namespace DMV {
	/// <summary>
	/// Wrapper for static members from different classes
	/// </summary>
	public class Remote : MarshalByRefObject {
		private static IE.IE ie = null;
		public static Remote Activate(){	return (Remote)Activator.GetObject(typeof(Remote),Dimok.Config.DMV.TcpPath);}

		public string[] FetchTitleByVIN(string vin){
			if( ie == null )ie = new Dimok.IE.IE();
			try{
				ie.Init(true,Dimok.Config.DMV.WebSiteUrl);
				ie.WaitIE();
				ie.SetElementValue("ObjectSpace$Global$0$VIN",vin);//"JH4UA2653WC004630");
				ie.GetElementByName("ObjectSpace$Global$0$dhsmvSubmitBtn_2").click();
				ie.WaitIE();
				StringList sl = new StringList();
				for( int i=1;;i++)
					try{
						IHTMLElement el = ie.GetElementByAttr("name","LH"+i+"Name");
						sl.Add(el.innerText);
					}catch(IE.HTMLElementNotFoundException){break;}
				sl.Add(IE.IE.InjectBASE(ie.doc.body.parentElement.outerHTML,ie.doc.url));
				return sl;
			}catch{
				return new StringList("Error. Try again.");
			}
		}

	}
	[Serializable]
	public struct TitleInfo{
		public string[] LienHolders;
		public string HTML;
		public TitleInfo(string html):this(new string[]{},html){}
		public TitleInfo(string[] lienHolders, string html){
			LienHolders = lienHolders;
			HTML = html;
		}
	}
}